package com.xuzhijie.pms.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.Owner;
import com.xuzhijie.pms.entity.Pet;
import com.xuzhijie.pms.entity.Property;
import com.xuzhijie.pms.entity.Vehicle;
import com.xuzhijie.pms.entity.pojo.params;
import com.xuzhijie.pms.entity.vo.PropertyVo;
import com.xuzhijie.pms.service.IOwnerService;
import com.xuzhijie.pms.service.IPetService;
import com.xuzhijie.pms.service.IPropertyService;
import com.xuzhijie.pms.service.IVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-16
 */
@RestController
@RequestMapping("/property")
public class PropertyController {

    @Autowired
    private IPropertyService service;

    @Autowired
    private IOwnerService iOwnerService;

    @Autowired
    private IVehicleService iVehicleService;

    @Autowired
    private IPetService iPetService;

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer page,
                           @RequestParam Integer size,
                           @RequestParam Integer type,
                           @RequestParam Integer checkIn,
                           @RequestParam String number) {

        return service.findPage(page, size, type, checkIn, number);
    }

    @GetMapping("/getOwnerAndProperty")
    public Result getOwnerAndProperty(@RequestParam String phone,
                                      @RequestParam Integer page,
                                      @RequestParam Integer size,
                                      @RequestParam String number) {
        return service.getOwnerAndProperty(phone, page, size, number);
    }

    @PutMapping("/binding")
    public Result bindingPropertyWithOwner(@RequestBody params params
    ) {
        System.out.println(params.getPropertyId() + "---------" + params.getOwnerid());

        String ownerid = params.getOwnerid();
        String propertyId = params.getPropertyId();
        return service.bindingPropertyWithOwner(ownerid, propertyId);
    }

    @GetMapping("/wx-getpropertyByOwnerId")
    public Result WxGetPropertyWithOwner(@RequestParam String id) {
        return service.WxGetPropertyWithOwner(id);
    }

    @GetMapping("/wx-getpropertyById")
    public Result WxGetPropertyById(@RequestParam String id) {
        Property property = service.getById(id);
        PropertyVo propertyVo = new PropertyVo(property);
        BeanUtil.copyProperties(property, propertyVo);
        return Result.success(propertyVo);
    }

    @GetMapping("/wx-getproperty4rs2")
    public Result WxGetPropertyWithOwnerWhereReno(@RequestParam String id, @RequestParam Integer renovationStatus) {
        return service.WxGetPropertyWithOwnerWhereReno(id, renovationStatus);
    }

    @GetMapping("/homeData")
    public Result homeInit() {
//
        List<Property> list = service.list();
        long residenceCount = list.stream().filter(p -> p.getCheckIn() == 1 && p.getType() == 3).count(); //已使用住宅
        long unResidenceCount = list.stream().filter(p -> p.getCheckIn() == 2 && p.getType() == 3).count();//未使用住宅
        long shopCount = list.stream().filter(p -> p.getCheckIn() == 1 && p.getType() == 4).count();//未使用住宅
        long unShopCount = list.stream().filter(p -> p.getCheckIn() == 2 && p.getType() == 4).count();//未使用住宅
        double propertyCount = ((double)(residenceCount + shopCount)*100) / (residenceCount + unResidenceCount + shopCount + unShopCount);


        //车辆和车库使用率
        long spacesCount = list.stream().filter(p -> p.getCheckIn() == 1 && p.getType() == 2).count(); //已使用车位
        long unSpacesCount = list.stream().filter(p -> p.getCheckIn() == 2 && p.getType() == 2).count(); //未使用使用车位
        long garageCount = list.stream().filter(p -> p.getCheckIn() == 1 && p.getType() == 5).count(); //已使用车位
        long unGarageCount = list.stream().filter(p -> p.getCheckIn() == 2 && p.getType() == 5).count(); //未使用使用车位
        double vehicleCount = ((double)(spacesCount + garageCount)*100) / (spacesCount + unSpacesCount + garageCount + unGarageCount);

        long unWarehouseCount = list.stream().filter(p -> p.getCheckIn() == 2 && p.getType() == 1).count();
        long warehouseCount = list.stream().filter(p -> p.getCheckIn() == 1 && p.getType() == 1).count();
        double warehouse = ((double)warehouseCount) / (unWarehouseCount+warehouseCount)*100;

        long relevancyCount = list.stream().filter(p -> p.getCheckIn() == 1).count();
        double rate = (double) relevancyCount / list.size()*100 ;

        long ownerCount = iOwnerService.count(new QueryWrapper<Owner>().eq("status", 1));
        long carCount = iVehicleService.count(new QueryWrapper<Vehicle>().eq("is_binding", 1));
        long petCount = iPetService.count(new QueryWrapper<Pet>().eq("is_check_in", 1));

        Map<String, Object> map = new HashMap<>();
        map.put("residenceCount", residenceCount);
        map.put("unResidenceCount", unResidenceCount);
        map.put("shopCount", shopCount);
        map.put("unShopCount", unShopCount);
        map.put("total", list.size());
        map.put("propertyCount", propertyCount);
        map.put("spacesCount", spacesCount);
        map.put("unSpacesCount", unSpacesCount);
        map.put("garageCount", garageCount);
        map.put("unGarageCount", unGarageCount);
        map.put("vehicleCount", vehicleCount);
        map.put("unWarehouseCount", unWarehouseCount);
        map.put("warehouseCount", warehouseCount);
        map.put("warehouse", warehouse);
        map.put("rate", rate);
        map.put("relevancyCount", relevancyCount);
        map.put("ownerCount",ownerCount);
        map.put("carCount",carCount);
        map.put("petCount",petCount);


        return Result.success(map);
    }

}

